﻿using EFModel;
using IServices;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using xm.Controllers;
using Y2.Content;

namespace Serives
{
    public class Teusers : Iusers
    {
        private readonly MyDBContext he;

        public Teusers(MyDBContext he)
        {
            this.he = he;
        }
        //角色删除
        public Task<int> Delt(users users)
        {
            return Task.Run(() => {
                users us = he.users.Find(users.u_id);
                he.users.Remove(us);
                int num = he.SaveChanges();
                return num;
            });
        }
        //当前登录人查询
        public Task<string> Fill(int id)
        {
            return Task.Run(() => {
                return JsonConvert.SerializeObject(he.users.Where(e=> e.u_id==id).ToList());
            });
        }
        //角色添加
        public Task<int> Insert(users users)
        {
            return Task.Run(() => {
                he.users.Add(users);
                int num = he.SaveChanges();
                return num;
            });
        }
        //用户查询
        public Task<string> que()
        {
            return Task.Run(() => {
                return JsonConvert.SerializeObject(he.users.ToList());
            });
        }
        //角色修改
        public Task<int> Update(users users)
        {
            return Task.Run(() => {
                users users1 = he.users.Find(users.u_id);
                users1.u_name = users.u_name;
                users1.u_password = users.u_password;
                users1.u_true_name = users.u_true_name;
                users1.RoleID = users.RoleID;
                int num = he.SaveChanges();
                return num;
            });
        }
        //角色查询
        public Task<ResultInfo> users(int currentPage, int pageSize)
        {
            return Task.Run(() => {
                var con = he.users;
                var list = he.users
                   .Select(e => new { e.u_id, e.u_name, e.u_password, e.u_true_name, e.Role.Rolename,e.RoleID })
                    .OrderBy(q => q.u_id).Skip((currentPage - 1) * pageSize).Take(pageSize)
                    .ToList();
                int cont = con.Count();
                ResultInfo result = new ResultInfo()
                {
                    Data = list,
                    Message = "查询数据成功",
                    Status = 200,
                    Row = cont
                };
                return result;
            });
        }
        //可用职位查询
        public Task<string> vs()
        {
            return Task.Run(() => {
                var i = he.Role.Where(e=> e.Ysn==1);
                return JsonConvert.SerializeObject(i);
            });
        }
        //登录查询
        Task<ResultInfos> Iusers.UseAll(string name, string pwd)
        {
            return Task.Run(() => {
                ResultInfos result = new ResultInfos();
                //假设用户名为"123"，密码为"123"  
                var num = he.users.Where(e => e.u_name == name && e.u_password == pwd).Select(s => s.u_id).ToList();
                if (num[0]!=0)
                {
                    var payload = new Dictionary<string, object>
                {
                    { "u_name",name },
                    { "u_password", pwd }
                };
                    result.Token = JWTHelper.SetJwtEncode(payload);
                    result.Success = true;
                    result.Message = "成功";
                }
                else
                {
                    result.Token = "";
                    result.Success = false;
                    result.Message = "生成token失败";
                }
                return result;
            });
        }
    }
}
